package com.xyh.jvmDemo;

import com.sun.org.apache.bcel.internal.generic.DDIV;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.xyh.jvmDemo
 * @类名称：RunVolatileDemo
 * @创建人：YIHANG.XU
 * @创建时间：2020/10/1510:10 下午
 * @修改人：YIHANG.XU
 * @修改时间：2020/10/1510:10 下午
 * @修改备注：
 * @Copyright
 */
public class RunVolatileDemo {
    public static void main(String[] args) {
        RunDemo1 runDemo1 = new RunDemo1();
        Thread thread1 = new Thread(runDemo1);
        Thread thread2 = new Thread(runDemo1);
        Thread thread3 = new Thread(runDemo1);
        thread1.start();
        thread2.start();
        thread3.start();
    }

}

class RunDemo1 implements Runnable {

    String dd = "aa";
    int ticket = 100;


    @Override
    public void run() {
        while (ticket > 0) {
            synchronized (dd) {
                if (ticket > 0) {
                    //窗口三有二分之一的可能是退票
                    System.out.println(Thread.currentThread().getName() + "卖了第" + ticket + "张票");
                    ticket--;
                } else {
                    System.out.println("很抱歉，票卖完了");
                }
                try {

                    Thread.sleep(100);
                } //休息一秒
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

